package com.cetccst.securitysuite.fragments.viewmodel;

import static com.cetccst.securitysuite.kv.SSKV.kvStore;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.cetccst.securitysuite.kv.KVKeys;
import com.cetccst.securitysuite.log.SSLogger;
import com.cetccst.securitysuite.network.HttpCallback;
import com.cetccst.securitysuite.network.LoginHttpRequester;
import com.cetccst.securitysuite.network.respone.TokenResponse;

public class LoginViewModel extends ViewModel {

    private static final String TAG = LoginViewModel.class.getSimpleName();
    public LiveData<String> phone = new MutableLiveData<>();

    public LiveData<String> password = new MutableLiveData<>();
    public MutableLiveData<Boolean> watermark = new MutableLiveData<>(false);

    public void login(LoginCallback callback) {
        LoginHttpRequester.getInstance().login(new HttpCallback<>() {
            @Override
            public void onResponse(TokenResponse data) {
                SSLogger.logger.error("login be called");
                if (data != null && data.getErrCode() == 0) {
                    kvStore.encode(KVKeys.LOGIN_ACCESS_TOKEN, "Bearer " + data.getToken().getAccessToken());
                }
                callback.response(data != null && data.getErrCode() == 0);
            }
        });
    }

    public interface LoginCallback {
        void response(boolean result);
    }
}
